﻿/**
* EaselJS
* Visit http://easeljs.com/ for documentation, updates and examples.
*
* Copyright (c) 2011 Grant Skinner
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
**/
(function(m){function a(){throw"SoundJS cannot be instantiated";}function n(a,d){this.instances=[];this.name=a;this.src=d;this.loaded=this.canPlay=false;this.length=0}a.AUDIO_TIMEOUT=8E3;a.INTERRUPT_ANY=1;a.INTERRUPT_EARLY=2;a.INTERRUPT_LATE=3;a.INTERRUPT_NONE=4;a.onProgress=null;a.onSoundTimeout=null;a.onSoundLoadError=null;a.onSoundLoadComplete=null;a.onLoadQueueComplete=null;a.onSoundComplete=null;a.soundHash=[];a.loadQueue=[];a.itemsToLoad=0;a.instanceCount=0;a.INST_MAX=35;a.FT=0.0010;a.AUDIO_ERROR=
"error";a.AUDIO_PROGRESS="progress";a.AUDIO_COMPLETE="canplaythrough";a.AUDIO_ENDED="ended";a.AUDIO_STALLED="stalled";a._master=1;a._currentLoad=0;a.add=function(b,d,c){a.loadQueue.push({name:b,src:d,instances:c});a.loadQueue.length==1?(a.itemsToLoad=1,a.loadNext()):a.itemsToLoad++};a.addBatch=function(b){for(var d=b.length;b.length;)a.loadQueue.push(b.shift());a.loadQueue.length==d?(a.loadNext(),a.itemsToLoad=d):a.itemsToLoad++};a.play=function(b,d,c,e,f){if(b==null||a.soundHash[b]==null||a.soundHash[b].length==
0||d!=a.INTERRUPT_ANY&&d!=a.INTERRUPT_EARLY&&d!=a.INTERRUPT_LATE&&d!=a.INTERRUPT_NONE&&d!=null)return NaN;if(d==null)d=a.INTERRUPT_NONE;e==null&&(e=false);f||(f=0);c==null||c>1?c=1:c<0&&(c=0);if(f>0)setTimeout(function(){a.beginPlaying(b,d,c,e)},f);else return a.beginPlaying(b,d,c,e);return-1};a.getMasterVolume=function(){return a._master};a.setMasterVolume=function(b){if(Number(b)!=null){b<0?b=0:b>1&&(b=1);var d,c,e;d=a._master;a._master=b;if(a._master!=d)for(e in a.soundHash){c=a.soundHash[e];d=
c.length;for(b=0;b<d;b++)c[b].volume=c[b].storedVolume*a._master}}};a.remove=function(b,d){var c,e,f;if(b==null)for(c in a.soundHash){f=a.soundHash[c];e=f.length;do a.stop(c,e-1),f[e-1].currentSrc="",document.body.removeChild(f[e-1]),f.pop(),e=f.length,a.instanceCount--;while(e)}else{f=a.soundHash[b];if(f==null)return false;e=f.length;if(d==null){do a.stop(b,e-1),f[e-1].currentSrc="",document.body.removeChild(f[e-1]),f.pop(),e=f.length,a.instanceCount--;while(e)}else{if(d<=0||e<=0)return false;e--;
for(c=0;c<=e&&c<d;c++)a.stop(b,e-c),f[e-c].currentSrc="",document.body.removeChild(f[e-c]),f.pop(),a.instanceCount--}}return true};a.setVolume=function(b,d,c){var e,f;if(b==null)return false;b*=a._master;if(d==null)for(f in a.soundHash){e=a.soundHash[f];d=e.length;for(c=0;c<d;c++)e[c].storedVolume=b,e[c].volume=b}else{e=a.soundHash[d];if(e==null)return false;d=e.length;if(c==null)for(c=0;c<d;c++)e[c].storedVolume=b,e[c].volume=b;else{if(d<=c)return false;e[c].storedVolume=b;e[c].volume=b}}return true};
a.getVolume=function(b,d){var c=a.soundHash[b];return c==null||c.length==0?-1:d==null?c[1].storedVolume:c.length<d?-1:c[d].storedVolume};a.setMute=function(b,d,c){var e,f;if(b==null)return false;if(d==null)for(f in a.soundHash){e=a.soundHash[f];d=e.length;for(c=0;c<d;c++)e[c].muted=b}else{e=a.soundHash[d];if(e==null)return false;d=e.length;if(c==null)for(c=0;c<d;c++)e[c].muted=b;else{if(d<=c)return false;e[c].muted=b}}return true};a.pause=function(b,d){var c,e,f,g;if(b==null)for(g in a.soundHash){f=
a.soundHash[g];e=f.length;for(c=0;c<e;c++)f[c].pause()}else{f=a.soundHash[b];if(f==null)return false;e=f.length;if(d==null)for(c=0;c<e;c++)f[c].pause();else{if(e<=d)return false;f[d].pause()}}return true};a.resume=function(b,d){var c,e,f,g;if(b==null)for(g in a.soundHash){e=a.soundHash[g].length;for(c=0;c<e;c++)f=a.soundHash[g][c],(f.loop||f.currentTime!=f.duration&&f.currentTime!=0)&&f.play()}else{if(a.soundHash[b]==null)return false;e=a.soundHash[b].length;if(d==null)for(c=0;c<e;c++)f=a.soundHash[b][c],
(f.loop||f.currentTime!=f.duration&&f.currentTime!=0)&&f.play();else{if(e<=d)return false;f=a.soundHash[b][d];(f.loop||f.currentTime!=f.duration&&f.currentTime!=0)&&f.play()}}return true};a.stop=function(b,d){var c,e,f,g;if(b==null)for(g in a.soundHash){e=a.soundHash[g].length;for(c=0;c<e;c++){f=a.soundHash[g][c];try{f.currentTime=0}catch(h){}f.pause()}}else{if(a.soundHash[b]==null)return false;e=a.soundHash[b].length;if(d==null)for(c=0;c<e;c++)f=a.soundHash[b][c],f.currentTime=0,f.pause();else{if(e<=
d)return false;f=a.soundHash[b][d];f.currentTime=0;f.pause()}}return true};a.isLoaded=function(b){var d=true,c;if(b==null)for(c in a.soundHash){if(d=d&&a.soundHash[c]&&a.soundHash[c][0]&&a.soundHash[c][0].loaded,!d)break}else return a.soundHash[b]&&a.soundHash[b][0]&&a.soundHash[b][0].loaded;return d};a.getNumInstances=function(b){return b==null?instanceCount:a.soundHash[b]?a.soundHash[b].length:-1};a.getMaxInstances=function(){return a.INST_MAX};a.getCurrentLoadProgress=function(){return(a.itemsToLoad-
a.loadQueue.length-(1-a._currentLoad))/a.itemsToLoad};a.getInstance=function(b,d){return b==null||d<0||!a.soundHash[b]||!a.soundHash[b][d]?null:a.soundHash[b][d]};a.beginPlaying=function(b,d,c,e){var f,g,h,i=false,j=a.soundHash[b];if(!j[0].loaded)throw Error("Audio is not loaded. The source(s) are either not found, or the correct audio formats are not provided.");for(var k=j.length,b=0;b<k;b++){h=j[b];g==null&&d!=a.INTERRUPT_ANY&&d!=a.INTERRUPT_NONE&&(g=h,f=b);if(h.currentTime>=h.duration-a.FT&&!h.loop||
h.currentTime==0&&h.paused)i=true,k=b;else if(d==a.INTERRUPT_EARLY&&h.currentTime<g.currentTime||d==a.INTERRUPT_LATE&&h.currentTime>g.currentTime)i=true;i&&(g=h,f=b)}d==a.INTERRUPT_ANY&&!g&&(g=j[0],f=0);return g?(g.loop=e,g.storedVolume=c,g.volume=c*a._master,g.currentTime=0,g.paused&&g.play(),f):-1};a.loadNext=function(){if(a.loadQueue.length<=0){if(a.onLoadQueueComplete)a.onLoadQueueComplete()}else{var b=a.loadQueue.shift(),d=b.instances||1,c=b.name,b=b.src,e=a.soundHash[c];if(e==null)e=a.soundHash[c]=
[];else if(e.length)b=e[0].src;for(var f=e=e.length,d=e+d;f<d;f++){var g=document.createElement("audio");if(f==e)g.timeoutId=setTimeout(function(){a.handleAudioTimeout(g)},a.AUDIO_TIMEOUT),g.addEventListener(a.AUDIO_COMPLETE,a.handleAudioComplete,false),g.addEventListener(a.AUDIO_PROGRESS,a.handleProgress,false),g.addEventListener(a.AUDIO_STALLED,a.handleAudioStall,false),g.addEventListener(a.AUDIO_ERROR,a.handleAudioError,false),g.loaded=false;g.addEventListener(a.AUDIO_ENDED,a.handleEnded,false);
g.setAttribute("id",c+"_"+f);g.setAttribute("preload","auto");var h;if(b instanceof Array)for(var i=0,j=b.length;i<j;i++){var k=g.appendChild(document.createElement("source"));h=a.getType(b[i]);k.setAttribute("type",h);k.setAttribute("src",b[i])}else h=a.getType(b),g.setAttribute("type",h),g.setAttribute("src",b);g.load();document.body.appendChild(g);a.soundHash[c].push(g);a.instanceCount++}}};a.getType=function(a){switch(a.slice(a.lastIndexOf(".")+1)){case "mp3":return"audio/mpeg";case "ogg":return"audio/ogg";
case "wav":return"audio/wav";default:throw"'"+a+"' is not a recognized audio file";}};a.handleEnded=function(){if(a.onSoundEnded){var b=this.id.split("_");a.onSoundEnded(this,b[0],b[1])}};a.handleAudioTimeout=function(b){if(a.onSoundTimeout){var d=b.id.split("_");a.onSoundTimeout(b,d[0],d[1])}a.loadNext()};a.handleProgress=function(){try{this.buffered.end()}catch(b){return}a._currentLoad=this.buffered.end()/this.duration;if(a.onProgress)a.onProgress(a.getCurrentLoadProgress())};a.handleAudioError=
function(){clearTimeout(this.timeoutId);if(a.onSoundLoadError){var b=this.id.split("_");a.onSoundLoadError(this,b[0],b[1])}a.loadNext()};a.handleAudioComplete=function(){var b=this.id.split("_");this.removeEventListener(a.AUDIO_COMPLETE,a.handleAudioComplete,false);clearTimeout(this.timeoutId);this.loaded=true;if(a.onSoundLoadComplete)a.onSoundLoadComplete(this,b[0],b[1]);a.loadNext()};a.handleAudioStall=function(a){setTimeout(function(){testAudioStall(a)},0)};a.testAudioStall=function(){var b=this.id.split("_");
if(!a.soundHash[b[0]][b[1]].loaded){if(a.onStall)a.onStall(this,b[0],b[1]);a.loadNext()}};m.SoundJS=a;var l=n.prototype;l.add=function(a){this.instances.push(a);this.length=this.instances.length;this.instances.length==1&&a.addEventListener("canplaythrough",function(){a.canplaythrough()})};l.remove=function(a){this.instances.splice(a,1);this.length=this.instances.length};l.canplaythrough=function(){this.loaded=true};m.SoundJSElement=n})(window);
